package com.jinSgr.md5_sm2_encryption

import android.content.Context
import android.os.Build
import android.util.Log
import androidx.annotation.NonNull
import androidx.annotation.RequiresApi
import com.jinSgr.md5_sm2_encryption.sm2.SM2EncDecUtils
import com.jinSgr.md5_sm2_encryption.sm2.Util
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import java.nio.charset.StandardCharsets
import java.security.MessageDigest
import java.security.NoSuchAlgorithmException


/** Md5Sm2EncryptionPlugin */
class Md5Sm2EncryptionPlugin: FlutterPlugin, MethodCallHandler,SunvuaEncrypationUtils.SunvuaEncrypationApi {
  /// The MethodChannel that will the communication between Flutter and native Android
  ///
  /// This local reference serves to register the plugin with the Flutter Engine and unregister it
  /// when the Flutter Engine is detached from the Activity
  private lateinit var channel : MethodChannel
  private lateinit var  context: Context;
  override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {

    context = flutterPluginBinding.applicationContext
    SunvuaEncrypationUtils.SunvuaEncrypationApi.setup(flutterPluginBinding.binaryMessenger,this)

  }

  override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {

  }

  override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
    SunvuaEncrypationUtils.SunvuaEncrypationApi.setup(binding.binaryMessenger,null)
  }

  @RequiresApi(Build.VERSION_CODES.O)
  override fun sEncrypation(arg: SunvuaEncrypationUtils.SunvuaEncrypationRequest?): SunvuaEncrypationUtils.SunvuaEncrypationResponse {
    var response =SunvuaEncrypationUtils.SunvuaEncrypationResponse()
    Log.d("我要开始加密了","${arg?.content}---${arg?.encryptiontype}---${arg?.privateKey}---${arg?.publicKey}---")
    arg?.let {
       response.content = SunvuaEncryptionUtils.md5bybase64bySm2(it.content,it.publicKey)
    }
    Log.d("加密结果","${response.content}")
    return response
  }
}
